<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl.class.php');
require_once(TANGRA_MAIN_DIR.'misc/image_manipulation_functions.inc.php');

require_once('image_upload_form.class.php');
require_once('image_upload_form_view.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_images_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/image/tcms_image_upload.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_dbc.class.php');


class Image_Upload_Form_Ctrl extends GForm_Ctrl {
	private $image_settings;
	private $dbc;
	private $uploads_dir;
	private $image_id;
	private $site_root_dir;

	function __construct($system_name, Vars_Manager $vm, TCMS_Settings_Images_DBC $image_settings, DB_Connection $dbc, $uploads_dir, $site_root_dir) {
		$this->image_settings = $image_settings;
		$this->dbc = $dbc;
		$this->uploads_dir = $uploads_dir;
		$this->site_root_dir = tangra_normalize_path($site_root_dir);

		parent::__construct($system_name, $vm);
	}


	protected function create_form() {
		$form = new Image_Upload_Form($this->image_settings->get_maximum_size());

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new Image_Upload_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		$file_prop = $this->form->get_field_value('image');

		if (TCMS_Image::is_valid_extension($file_prop['name'], $this->image_settings->get_allowed_extensions())) {
			$image_resource = tangra_create_image_resource_from_file($file_prop['tmp_name']);
			if ($image_resource) {
				$size_x = imagesx($image_resource);
				$size_y = imagesy($image_resource);

				if ($size_x <= $this->image_settings->get_maximum_size_x()) {
					if ($size_y <= $this->image_settings->get_maximum_size_y()) {
						$xy_ratio = $size_x / $size_y;
						if ((($this->image_settings->get_minimum_ratio_xy()) < $xy_ratio
								&& ($xy_ratio < $this->image_settings->get_maximum_ratio_xy()))) {

								$ci_id = $this->get_permanent_external_param('ci', true);
								try {
									$this->dbc->start_trans();
									$this->image_id = TCMS_Image_Upload::process_uploaded_image($this->dbc,
																								$file_prop,
																								$this->uploads_dir,
																								$this->image_settings,
																								$this->site_root_dir,
																								$ci_id,
																								$this->get_permanent_external_param('user_id')
																								);
									$this->dbc->complete_trans();
								} catch (Exception $e) {
									$this->dbc->fail_trans();
									$this->dbc->complete_trans();
									throw $e;
								}
						} else {
							$ret = false;
							$this->export('xy_range_min', $this->image_settings->get_minimum_ratio_xy());
							$this->export('xy_range_max', $this->image_settings->get_maximum_ratio_xy());
							$this->export('ratio_current', $xy_ratio);
							$this->form->set_field_error('image', 'xy_invalid_ratio');
						}
					} else {
						$ret = false;
						$this->export('maximum_height_allowed', $this->image_settings->get_maximum_size_y());
						$this->export('maximum_height_current', $size_y);
						$this->form->set_field_error('image', 'maximum_height_exceeded');
					}

				} else {
					$ret = false;
					$this->export('maximum_width_allowed', $this->image_settings->get_maximum_size_x());
					$this->export('maximum_width_current', $size_x);
					$this->form->set_field_error('image', 'maximum_width_exceeded');
				}
			} else {
				$ret = false;
				$this->form->set_field_error('image', 'invalid_file');
			}
		} else {
			$ret = false;
			$this->export('allowed_extensions', $this->image_settings->get_allowed_extensions());
			$this->form->set_field_error('image', 'invalid_extension');
		}


		if (!$ret) { // cleaning uploaded file (this is (over)tidy. PHP will delete it automatically but only if not crashes by some reason (segfault for example))
			if (file_exists($file_prop['tmp_name'])) {
				unlink($file_prop['tmp_name']);
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {
		$errors = $this->form->get_field_errors('image');
		if ($errors[0] == 'size_exceeded') {
			$file_prop = $this->form->get_field_value('image');

			$this->export('maximum_size_allowed', $this->image_settings->get_maximum_size());
		}

		$ci_id = $this->get_permanent_external_param('ci', true);
		if ($ci_id) {
			$this->export('ci_id', $ci_id);
		}
	}

}